﻿Public Class DecimalArgHandler
    Implements IArgHandler

    Public Function ValidateArgument(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition, value As String) As String Implements IArgHandler.ValidateArgument
        Dim val As Decimal = 0

        'make sure it's actually an integer
        If Not Decimal.TryParse(value, val) Then
            Return "Value must be an integer."
        End If

        'check the data rules
        Dim parts() As String = argument.TypeData.Split(" "c)
        Dim min As Decimal = Decimal.Parse(parts(0))
        Dim max As Decimal = Decimal.Parse(parts(1))

        If val < min OrElse val > max Then
            Return String.Format("Value must be between {0} and {1}.", min, max)
        End If

        'All validation passed
        Return Nothing
    End Function

    Public Function ValidateTypeData(typeData As String) As Boolean Implements IArgHandler.ValidateTypeData
        'decimal validation rules look like "minValue maxValue"

        'empty rules are valid
        If typeData = "" Then
            Return True
        End If

        Dim parts() As String = typeData.Split(" "c)
        'make sure it has 2 parts
        If parts.Count <> 2 Then
            Return False
        End If

        'make sure the parts are both integer
        Dim min As Decimal
        If Not Decimal.TryParse(parts(0), min) Then
            Return False
        End If
        Dim max As Decimal
        If Not Decimal.TryParse(parts(1), max) Then
            Return False
        End If

        'make sure min is less than or equal to max
        If min > max Then
            Return False
        End If

        'All validation passed
        Return True
    End Function

    Public Function GetValues(obj As RawObject, argument As ArgumentDefinition) As System.Collections.Generic.IEnumerable(Of String) Implements IArgHandler.GetValues
        Return New List(Of String)
    End Function

    Public ReadOnly Property TypeDataFormat As String Implements IArgHandler.TypeDataFormat
        Get
            Return ""
        End Get
    End Property

    Public ReadOnly Property DefaultTypeData As String Implements IArgHandler.DefaultTypeData
        Get
            Return ""
        End Get
    End Property

    Public ReadOnly Property DefaultValue(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition) As String Implements IArgHandler.DefaultValue
        Get
            Return "0"
        End Get
    End Property

    Public Function Parse(argParts As System.Collections.Generic.IEnumerator(Of String), arg As ArgumentDefinition) As String Implements IArgHandler.Parse
        Return argParts.Current
    End Function

    Public Function GetNavList(library As RawLibrary, obj As RawObject, argument As ArgumentDefinition, value As String) As IEnumerable(Of NavPoint) Implements IArgHandler.GetNavList
        Return Nothing
    End Function
End Class
